import pd from './pd.js'

function req(args) {
	uni.showLoading({
		title: '加载中'
	})
	args.sign = pd.sign(args);
	return new Promise((resolve, reject) => {
		uni.request({
			url: 'https://gw-api.pinduoduo.com/api/router',
			data: args,
			method: 'GET',
			headers: {
				"Content-Type": "application/json",
			},
			success(res) {
				resolve(res.data)
			},
			fail(e) {
				reject(e)
			},
			complete() {
				uni.hideLoading()
			}
		})
	})
}



/**搜索
 * @param {Object} q
 * @param {Object} page
 */
export function search(q, page) {
	return req({
		client_id: pd.CLIENTID,
		data_type: "JSON",
		sort_type: 0,
		timestamp: pd.timestamp(),
		type: "pdd.ddk.goods.search",
		pid: pd.PDDPID,
		page: page || 1,
		opt_id: 8569,
		block_cat_packages: `[1,2]`,
		with_coupon: false,
		keyword: '二手'+q
	}).then((r) => {
		if (r.goods_search_response && r.goods_search_response.goods_list) {
			let res = r.goods_search_response.goods_list
			return res.map(v => {
				return {
					v: false,
					pay: false,
					mall_id:v.mall_id,
					mall_name:v.mall_name,
					desc:v.goods_desc,
					type: '',
					mold: [],
					service: v.unified_tags,
					oldPrice:v.min_normal_price/100,
					price: v.min_group_price / 100,
					servicePlus: '',
					username: v.mall_name,
					time: Math.floor(Math.random() * 30 + 1) + '天发布',
					title: v.goods_name,
					img: v.goods_image_url,
					avatar: v.goods_thumbnail_url,
					search_id: v.search_id,
					goods_id: v.goods_id,
					goods_sign:v.goods_sign
				}
			})
		}
	})
}
/**
 * 详情
 */
export function info({
	goods_sign,
	search_id
}) {
	return req({
		client_id: pd.CLIENTID,
		data_type: "JSON",
		timestamp: pd.timestamp(),
		type: "pdd.ddk.goods.detail",
		pid: pd.PDDPID,
		goods_sign: goods_sign,
		search_id: search_id,
	}).then(res => {
		if (res.goods_detail_response && res.goods_detail_response.goods_details) {
			return res.goods_detail_response.goods_details[0]
		}
	})
}
